<template>
  <div class="list">
    <div class="content-head" style="margin-bottom: 20px;">
      <div class="sort-keys">

        <div class="tab-list">
           <div class="item" @click="status = item.key" :class="{actived: status === item.key}"
             v-for="(item, index) in statusList">
             {{item.text}}
           </div>
         </div>


        <a-input placeholder="标题/货运单号" style="width: 180px;margin-right:10px;" v-model="key" allowClear :size="size"
          @input="onSearch" />

        <a-select style="width: 150px;margin-right:10px;" v-model="admin_id" placeholder="采购员" allowClear v-if="action_show">
          <a-select-option :value="item.id" v-for="item in admin_list" :key="item.id">{{ item.title }}</a-select-option>
        </a-select>
        <a-checkbox :checked="isOutPriceCheck ? true : false" @change="onIsOutPriceCheck" :size="size">是否只看溢价</a-checkbox>
        
        <a-button :size="size" type="primary" @click="refreshData" style="margin-right:10px;">刷新</a-button>
        

      </div>

      <div style="margin-top: 4px;">  
        <a-button type="primary" style="margin-right:10px;"  @click="handleCreate">创建采购</a-button>
        <a-button style="margin-left: 30px; float: right;" type="primary" @click="isExport2 = !isExport2">
          <span>采购查询</span>
        </a-button>
      </div>
    </div>
    <a-table class="table" :rowKey="row => row.uuid" :dataSource="list" :pagination="pagination" :size="size">
      
      <a-table-column title="标题" width="120px">
        <template slot-scope="row">
          <div>{{row.title}}</div>
        </template>
      </a-table-column>

      <a-table-column title="数量" width="30px">
        <template slot-scope="row">
          <div>{{row.num}}个</div>
        </template>
      </a-table-column>
      
      <a-table-column title="商城价格" width="120px">
        <template slot-scope="row">
          <div>{{row.product_price}}</div>
        </template>
      </a-table-column>
      
      <a-table-column title="采购单价" width="120px">
        <template slot-scope="row">
          <div>{{(row.money_price / row.num).toFixed(2)}}</div>
        </template>
      </a-table-column>

      <a-table-column title="货物总价" width="80px">
        <template slot-scope="row">
          <div :style="{ color: parseFloat(row.money_price) > parseFloat(row.product_total_price) ? 'red' : 'inherit' }">{{row.money_price}}</div>
        </template>
      </a-table-column>
      
      <a-table-column title="溢价金额" width="80px">
        <template slot-scope="row">
          <div v-if="parseFloat(row.money_price) > parseFloat(row.product_total_price)" style="color:red">{{(row.money_price - row.product_total_price).toFixed(2)}}</div>
        </template>
      </a-table-column>

      <a-table-column title="货运公司" width="100px">
        <template slot-scope="row">
          <div>{{row.express_name}}</div>
        </template>
      </a-table-column>

      <a-table-column title="货运单号" width="100px">
        <template slot-scope="row">
          <div>{{row.express_number}}</div>
        </template>
      </a-table-column>

      <a-table-column title="备注" width="100px">
        <template slot-scope="row">
          <div>{{row.remark}}</div>
        </template>
      </a-table-column>
      <a-table-column title="付款附件" width="80px">
        <template slot-scope="row">
          <a v-if="row.image" target="_blank" :href="row.image">
            <img class="account-qrcode" :src="row.image" />
          </a>
        </template>
      </a-table-column>
      <a-table-column title="签收附件" width="80px">
        <template slot-scope="row">
          <a v-if="row.image2" target="_blank" :href="row.image2">
            <img class="account-qrcode" :src="row.image2" />
          </a>
        </template>
      </a-table-column>
       <a-table-column title="状态" width="100px">
        <template slot-scope="row">
          <div v-if="row.status == 1">物流中</div>
          <div v-else-if="row.status == 2">已入库</div>
          <div v-else-if="row.status == 3">已取消</div>
          <div v-else>已下单</div>
        </template>
      </a-table-column>
      <a-table-column title="创建日期" dataIndex="created_at" width="80px">
        <template slot-scope="created_at">
          <span>{{ created_at }}</span>
        </template>
      </a-table-column>
      <a-table-column title="入库时间" dataIndex="end_time" width="80px" v-if="status == 2 || status == 'all'">
        <template slot-scope="end_time">
          <span>{{ end_time }}</span>
        </template>
      </a-table-column>
      <a-table-column title="取消时间" dataIndex="cancel_time" width="80px" v-if="status == 3 || status == 'all'">
        <template slot-scope="cancel_time">
          <span>{{ cancel_time }}</span>
        </template>
      </a-table-column>
      <a-table-column title="创建者" width="100px">
        <template slot-scope="row">
          <div v-if="row.admin_name">{{row.admin_name}}</div>
        </template>
      </a-table-column>
       <a-table-column title="结束者" width="100px">
        <template slot-scope="row">
          <div v-if="row.admin_name2">{{row.admin_name2}}</div>
        </template>
      </a-table-column>
      <a-table-column title="操作" width="100px">
        <template slot-scope="row">
          <span>
            <a href="javascript:;" @click="handleEdit(row,1)" v-if="row.status === 1 && action_show">修改</a>
          <a-divider type="vertical" />
          <a href="javascript:;" @click="handleEdit(row,2)" v-if="row.status === 1 && action_show">入库</a>
          <a-divider type="vertical" />
          <a href="javascript:;" @click="handleEdit(row,3)" v-if="row.status === 1 && action_show">取消</a>
          </span>
        </template>
      </a-table-column>
    </a-table>
    <ExportPopup2 @hide="isExport2=false" :isShow="isExport2"></ExportPopup2>
  </div>
</template>
<script>
  import moment from 'moment'
  import ISwitch from '@/components/ISwitch/ISwitch'
  import ExportPopup2 from "./components/ExportPopup2"
  const resourceType = 'storage_buy'

  export default {
    components: {
      ISwitch,
      ExportPopup2,
    },
    props: {
      selectMode: {
        type: Boolean
      },
      params: {
        type: Object
      },
      size: {
        type: String,
        default: 'default'
      }
    },
    data() {
      return {
        isOutPriceCheck: false,
        action_show: false,
        admin_list: [],
        admin_id: undefined,
        isExport2: false,
        isEdit: false,
        editUuid: '',
        list: [],
        page: 1,
        per_page: 10,
        total: 0,
        key: '',
        status: '1',
        frame: [
          {
            title: '标题',
            key: 'title',
            newline: true,
            required: true,
          },
          {
            title: '数量',
            key: 'num',
            type: 'number',
            newline: true,
            required: true,
          },
          {
            title: '货物总价',
            key: 'money_price',
            type: 'number',
            newline: true,
            required: true,
          },
          {
            title: '货运公司',
            key: 'express_name',
            newline: true,
          },
          {
            title: '货运单号',
            key: 'express_number',
            newline: true,
          },
          {
            title: '备注',
            type: 'textarea',
            key: 'remark',
            span: 22,
          },
          {
            title: '付款附件',
            type: 'image',
            key: 'image',
            showLibrary: false,
            required: true,
            span: 22,
          },
          {
            title: '状态',
            key: 'status',
            type: 'radio',
            default: 1,
            required: true,
            span: 24,
            options: [
              {
                title: '物流中',
                value: 1,
              },
            ],
          },
        ],
        frame2: [
          {
            title: '标题',
            key: 'title',
            newline: true,
            required: true,
            disabled:true,
          },
          {
            title: '数量',
            key: 'num',
            type: 'number',
            newline: true,
            required: true,
            disabled:true,
          },
          {
            title: '货物总价',
            key: 'money_price',
            type: 'number',
            newline: true,
            required: true,
            disabled:true,
          },
          {
            title: '签收附件',
            type: 'image',
            key: 'image2',
            showLibrary: false,
            span: 22,
          },
          {
            title: '状态',
            key: 'status',
            type: 'radio',
            default: 2,
            required: true,
            span: 24,
            options: [
              {
                title: '已入库',
                value: 2,
              }
            ],
          },
        ],
        frame3: [
          {
            title: '标题',
            key: 'title',
            newline: true,
            required: true,
            disabled:true,
          },
          {
            title: '数量',
            key: 'num',
            type: 'number',
            newline: true,
            required: true,
            disabled:true,
          },
          {
            title: '货物总价',
            key: 'money_price',
            type: 'number',
            newline: true,
            required: true,
            disabled:true,
          },
          {
            title: '取消附件',
            type: 'image',
            key: 'image2',
            showLibrary: false,
            required: true,
            span: 22,
          },
          {
            title: '状态',
            key: 'status',
            type: 'radio',
            default: 3,
            required: true,
            span: 24,
            options: [
              {
                title: '已取消',
                value: 3,
              }
            ],
          },
        ],
        frame_bf: [],
        row_bf: [],
        statusList: [
          {
            text: '物流中',
            key: '1'
          },
          {
            text: '已入库',
            key: '2'
          },
          {
            text: '已取消',
            key: '3'
          },
          {
            text: '全部',
            key: 'all'
          },
        ],
      }
    },
    watch: {
      params() {
        this.refreshData()
      },
      admin_id() {
        this.refreshData()
      },
      status() {
        this.refreshData()
      }
    },
    computed: {
      pagination() {
        return {
          defaultPageSize: this.per_page,
          defaultCurrent: this.page,
          total: this.total,
          onChange: (e) => {
            this.page = e
            this.fetchData()
          }
        }
      }
    },
    filters: {},
    created() {
      this.action_show = this.$authCheck('storage_buylist')
      this.$http.get('/storage_admin_list').then(res => {
        this.admin_list = res.data.list
        this.admin_list = this.admin_list.map(item => {
          return {
            title: item.name,
            id: item.admin_id
          }
        })
      })
      this.fetchData()
    },
    methods: {
      handleDelete(row) {
        this.$confirm({
          title: '提示',
          content: `确认删除吗？`,
          onOk: () => {
            this.$http.delete(`/${resourceType}/${row.uuid}`).then(res => {
              this.fetchData()
            })
          }
        })
      },
      onIsOutPriceCheck (e) {
        this.isOutPriceCheck = e.target.checked
        this.refreshData()
      },
      handleCreate() {
        this.$iForm.show({
          title: '新增',
          frame: this.frame,
          success: (data) => {
            this.$http.post(`/storage_buy`, {
              ...data,
            }).then(res => {
              this.fetchData()
              this.$iForm.hide()
            })
          },
        })
      },
      handleEdit(row,status) {
        if(status == 2){
          this.frame_bf = this.frame2
          this.row_bf = Object.assign({}, row)
          this.row_bf.status = 2
        }else if(status == 3){
          this.frame_bf = this.frame3
          this.row_bf = Object.assign({}, row)
          this.row_bf.status = 3
        }else{
          this.frame_bf = this.frame
          this.row_bf = Object.assign({}, row)
        }
        this.$iForm.show({
          title: '编辑',
          frame: this.frame_bf,
          form: this.row_bf,
          success: (data) => {
            this.$http.put(`/storage_buy/${row.uuid}`, {
              type: 'update',
              attributes: {
                ...data
              }
            }).then(res => {
              this.fetchData()
              this.$iForm.hide()
            })
          },
        })
      },
      refreshData() {
        this.page = 1
        this.key = ''
        this.fetchData()
      },
      onSearch(e) {
        this.fetchData()
      },
      fetchData() {
        this.isEdit = false
        this.$http.get(`/${resourceType}`, {
          params: {
            ...this.params,
            page: this.page,
            per_page: this.per_page,
            key: this.key,
            status: this.status,
            admin_id: this.admin_id,
            is_out_price_check: this.isOutPriceCheck,
          }
        }).then(res => {
          this.list = res.data.list
          this.total = res.data.item_total
        }).finally(() => {

        })
      },
    }
  }
</script>
<style lang="scss" scoped>
  .item-key {
    font-size: 86%;
    color: gray;
  }
  .account-qrcode {
    width: 40px;
    cursor: pointer;
  }
  .sort-keys {
    display: flex;
    align-items: center;

    .tab-list {
      display: flex;
      margin-top: 6px;
      border-bottom: 1px solid #e1e1e1;
      margin-right: 50px;

      .item {
        margin-right: 30px;
        padding: 0px 10px 4px 10px;
        cursor: pointer;
        font-weight: 500;

        &.actived {
          color: #1890FF;
          border-bottom: 3px solid #1890FF;
        }

        &:hover {
          color: #1890FF;
        }
      }
    }

  }

  .top-btn {
    background: #1890FF;
    color: white;
    cursor: pointer;
    font-size: 80%;
    border-radius: 4px;
    padding: 2px 6px;

    &.unset {
      border: 1px solid #D8D5D5;
      background: #D8D5D5;
      color: gray;
    }
  }

  .content-head {
    display: flex;
    justify-content: space-between;
    flex-wrap: wrap;
  }

  .table {
    .thumb {
      height: 60px;
      border-radius: 6px;
    }

    .content-image {
      height: 60px;
      width: 60px;
      margin-right: 10px;
      margin-bottom: 10px;
      object-fit: cover;
      border-radius: 10px;
      cursor: pointer;

      &:hover {
        opacity: 0.8;
      }
    }

    .user {
      display: flex;
      // flex-wrap: wrap;
      align-items: center;

      img {
        width: 26px;
        height: 26px;
        margin-right: 6px;
        border-radius: 50%;
        overflow: hidden;
      }

      .gender-man,
      .gender-woman {
        margin-left: 5px;
        font-size: 12px;
      }

      .gender-man {
        color: #409EFF;
      }

      .gender-woman {
        color: #F56C6C;
      }
    }
  }
</style>
